package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAddress;
import com.qf.pojo.DtsBrand;
import com.qf.service.AdminService;
import com.qf.service.BrandService;
import com.qf.service.CategoryService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private CategoryService categoryService;


    //http://localhost:8080/admin/brand/list?
    //page=1&limit=20&id=1&name=aa&sort=add_time&order=desc
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String name,Integer id,String sort,String order) {

        IPage<DtsBrand> iPage = brandService.findListByQuery(page,limit,name,id,sort,order);

        Map data=new HashMap();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @GetMapping("/catAndAdmin")
    public Object catAndAdmin(){
        List<CatVo> adminList=adminService.findAll();
        List<CatVo> categoryList = categoryService.findAll();


        Map data=new HashMap();
        data.put("adminList",adminList);
        data.put("categoryList",categoryList);
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsBrand dtsBrand){
        brandService.create(dtsBrand);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand dtsBrand){
        brandService.update(dtsBrand);
        return ResponseUtil.ok();
    }
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理","品牌制造商"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand dtsBrand){
        brandService.delete(dtsBrand);
        return ResponseUtil.ok();
    }




}
